<template>
  <div>
    <div id="3dscene"></div>
    <audio :src="bgmSrc" type="audio/mpeg" loop id="speaker"></audio>
  </div>
</template>

<script>
import Bus from "@/lib/bus";
import { CLOUDSETTING, URLHEAD } from "@/config/global.js";
import cloudRenderer from "superapi-51world";

export default {
  name: "",
  data() {
    return {
      config: CLOUDSETTING,
      bgmSrc: "",
      sfcWindow: null,
    };
  },
  created() {},
  mounted() {
    this.startCloudRenderServer();
  },
  methods: {
    startCloudRenderServer() {
      var cloudRender = new cloudRenderer("3dscene", 0);
      window.cloudRender = cloudRender;
      fetch(`${this.config.url}/autoLogin`, {
        method: "POST",
        body: `username=${this.config.username}&pwd=${this.config.password}`,
        headers: { "Content-type": "application/x-www-form-urlencoded" },
      })
        .then((res) => res.json())
        .then((json) => {
          fetch(`${this.config.url}/Renderers/Any/${this.config.guid}`, {
            method: "POST",
            headers: {
              "Content-type": "application/x-www-form-urlencoded",
              Authorization: `Bearer ${json.token}`,
            },
            body: `width=${window.innerWidth}&height=${window.innerHeight}`,
          })
            .then((res) => res.json())
            .then((json) => {
              cloudRender.SuperAPI("StartRenderCloud", json.url);
              cloudRender.SuperAPI(
                "RegisterCloudResponse",
                this.cloudRenderListener
              );
            });
        })
        .catch((error) => {
          alert(error);
        });
    },
    cloudRenderListener(e) {
      console.log("收到的事件: " + e);
      let jsonObject = JSON.parse(e);
      switch (jsonObject.func_name) {
        case "APIAlready":
          // 3D世界加载完成 do Something
          window.cloudRender.SuperAPI("SetMultiRendering", {
            screenpercentage: 0.5,
          });
          Bus.$emit("3d-world-ready");
          break;

        case "OnPOIClick":
          console.log("OnPOIClick ID: " + jsonObject.args.id);
          Bus.$emit("poi-clicked", {
            id: jsonObject.args.id,
          });
          // 返回一个点击POI的ID值 do Something
          break;

        case "OnCameraMoveStart":
          setTimeout(() => {
            cloudRender.SuperAPI("SetEnvTime", { env_time: "08:00" });
          }, 10000);
          setTimeout(() => {
            cloudRender.SuperAPI("SetEnvTime", { env_time: "14:00" });
          }, 20000);
          setTimeout(() => {
            cloudRender.SuperAPI("SetEnvTime", { env_time: "17:00" });
          }, 30000);
          setTimeout(() => {
            cloudRender.SuperAPI("SetEnvTime", { env_time: "19:00" });
          }, 50000);
          break;

        case "OnCameraMoveEnd":
          Bus.$emit("open-ecology-panel", true);
          cloudRender.SuperAPI("SetEnvTime", { env_time: "08:00" });
          break;

        case "OnCustomWebJsEvent":
          Bus.$emit("window-event", {
            eventName: jsonObject.args.name,
            data: jsonObject.args.args,
          });
          Bus.$emit("maintain-effect", {
            eventName: jsonObject.args.name,
            data: jsonObject.args.args,
          });
          break;
      }
      return e;
    },
  },
};
</script>

<style lang="less" scoped>
</style>